﻿using KingerRobot.MexicoSailunMES.Dao;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;

namespace KingerRobot.MexicoSailunMES.Spi.DTO;
public class InCuringDto
{
    public bool IsSuccess { get; set; }
    public string Error { get; set; }

    public string TireCode { get; set; }
    public TireInfo TireInfo { get; set; }
    public List<TcmInfo> TcmInfos { get; set; } = new List<TcmInfo>();
}

public class TireInfo
{
    public string TireCode { get; set; }
    public int Height { get; set; }
    public double Size { get; set; }
    public string MaterialCode { get; set; }
    public long MaterialId { get; set; }
    public string MaterialDesc { get; set; }
    public string TbmNo { get; set; }
    public string TbmPs { get; set; }
    public string InPermit { get; set; }
    public DateTime ProdDt { get; set; }
    public DateTime? TbmExpirationTime { get; set; }
    public string ErrCd { get; set; }
    public int TireLock { get; set; }

    public long MesId { get; set;}

    public static TireInfo Create(POBcdInfo bcdInfo)
    {
        return new TireInfo()
        {
            TireCode = bcdInfo.BcdNo,
            MaterialCode = bcdInfo.TbmSpec,
            MaterialDesc = bcdInfo.TbmSpecDesc,
            Height = bcdInfo.HEIGHT ?? 0,
            Size = GetSize(bcdInfo.TbmSpecDesc),
            ProdDt = bcdInfo.ProdDt.Value,
            TbmNo = bcdInfo.TbmNo,
            TbmPs = bcdInfo.TbmPs,
            InPermit = bcdInfo.InPermit,
            TbmExpirationTime = bcdInfo.TbmExpirationTime,
           // ErrCd= bcdInfo.e
        };
    }


    /// <summary>
    /// 从规格描述中获取字母R后面的数字，即为尺寸
    /// </summary>
    /// <returns></returns>
    public static double GetSize(string materialDesc)
    {
        Match match = Regex.Match(materialDesc, @"R(\d+(?:\.\d+)?)");
        if (match.Success)
        {
            string text = match.Groups[1].Value;
            if (double.TryParse(text, out double size))
            {
                return size;
            }
        }

        return 0;
    }
}

public class TcmInfo
{
    public string TCM_NO { get; set; }
    public string TCM_DIR { get; set; }
    public string TCM_DESC { get; set; }
    public string TCM_SPEC { get; set; }
    public string TCM_SPEC_DESC { get; set; }
    public string TBM_SPEC { get; set; }
    public string TBM_SPEC_DESC { get; set; }
    public string TBM_SPEC_VER { get; set; }
    public string TBM_PS { get; set; }
    public int TCM_ANGLE { get; set; }
    public string TCM_HEIGHT { get; set; }
    public DateTime? REG_DT { get; set; }
    public string RCV_FLAG { get; set; }
    public string TASK_TYPE { get; set; }
    public string TURNOVER { get; set; }
    public string TASKCODE { get; set; }
    public string EXEC_RESULT { get; set; }

    public string REQ_LD_GT { get; set; }
    public string REQ_CV_GT { get; set; }
    public string REQ_GT_REQ { get; set; }
    public DateTime? REQ_REG_DT { get; set; }
    public DateTime? REQ_SULFOPEN_TIME { get; set; }

    public string OrderMsg { get; set; }

    /// <summary>
    /// 实际所需要胎的数量（计划要胎的数量 - 在途胎数量 - 订单数量）
    /// </summary>
    public int ActualRequireAmount { get; set; }
    public int Priority { get; set; }

    public static TcmInfo Create(POTcmGtSpec spec, POTcmGtReq req)
    {
        return new TcmInfo()
        {
            TCM_NO = spec.TcmNo,
            TCM_DIR = spec.TcmDir,
            TCM_DESC = spec.TcmDesc,
            TCM_SPEC = spec.TcmSpec,
            TCM_SPEC_DESC = spec.TcmSpecDesc,
            TCM_ANGLE = Convert.ToInt32(spec?.TcmAngle ?? 0),
            TCM_HEIGHT = spec.TcmHeight,
            TBM_SPEC = spec.TbmSpec,
            TBM_SPEC_DESC = spec.TbmSpecDesc,
            TBM_PS = spec.TbmPs,
            TBM_SPEC_VER = spec.TbmSpecVer,
            REQ_REG_DT = spec.RegDt,
            RCV_FLAG = spec.RcvFlag,
            TASK_TYPE = spec.TaskType,
            TURNOVER = spec.Turnover,
            //TASKCODE = spec.TASKCODE,
            //EXEC_RESULT = spec.EXECRESULT,
            REQ_LD_GT = req.LdGt,
            REQ_CV_GT = req.CvGt,
            REQ_GT_REQ = req.GtReq,
            REG_DT = req.RegDt,
            REQ_SULFOPEN_TIME = req.SulfopenTime,
            OrderMsg = $"{req.CvGt}-{req.LdGt}-{req.GtReq}"
        };
    }
}
